package com.sinodata.bsm.cicp.finder.sqlserver;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.sinodata.bsm.cicp.finder.Finder;
import com.sinodata.bsm.cicp.util.DBConnectionUtil;

/**
 * 
 * <p>
 * Description: SQL Server日志发现程序
 * </p>
 *
 * @author wangpeng
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2012-6-14 下午05:51:39          wangpeng        1.0         To create
 * </p>
 *
 * @since 
 * @see
 */
public class Log extends Finder {

    private static final Logger logger = Logger.getLogger(Log.class);

    @SuppressWarnings("unchecked")
    @Override
    public List<String[]> find(String ip, Map params) {
        List<String[]> list = new ArrayList<String[]>();
        String serverName = (String) params.get("SERVERNAME");
        CallableStatement statement = null;
        ResultSet rst = null;
        Connection conn = null;
        try {
            conn = DBConnectionUtil.getInstance().getConnection("sqlserver", ip, serverName);
            if (conn != null) {
                statement = conn.prepareCall("{call master.dbo.xp_enumerrorlogs}");
                statement.execute();
                rst = statement.getResultSet();
                int count = 0;
                while (rst.next()) {
                    if (count > 5) {
                        break;
                    }
                    int logID = rst.getInt(1); //resource name
                    String logName = null;
                    if (logID == 0)
                        logName = "Current";
                    else
                        logName = "Archive#" + logID;
                    String[] logInfo = new String[4];
                    list.add(logInfo);
                    logInfo[0] = serverName + "_" + logName;
                    logInfo[1] = logName;
                    logInfo[2] = logName + " in " + serverName;
                    logInfo[3] = logName; //日志名称
                    count++;
                }
                return list;
            } else {
                return null;
            }
        } catch (Exception ex) {
            logger.error("failed to find sqlserver log", ex);
            return null;
        } finally {
            if (rst != null) {
                try {
                    rst.close();
                } catch (SQLException ex) {
                    logger.error("failed to close rst", ex);
                }
            }
            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException ex) {
                    logger.error("failed to close statement", ex);
                }
            }
            DBConnectionUtil.getInstance().returnConnection(conn);
        }
    }

}
